<template>
  <div class="checkout-address">
    <div class="text">
      <div  v-if="!defaultAddress.id" class="none">您需要先添加收货地址才可提交订单。</div>
      <template v-else>
        <ul>
        <li><span>收<i/>货<i/>人：</span>{{ defaultAddress.receiver }}</li>
        <li><span>联系方式：</span>{{ defaultAddress.contact }}</li>
        <li><span>收货地址：</span>{{ defaultAddress.fullLocation + defaultAddress.address }}</li>
      </ul>
       <a href="javascript:;" @click="addAddressBtn(defaultAddress)">修改地址</a>
      </template>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="dialogVisible = true">切换地址</XtxButton>
      <XtxButton class="btn" @click="addAddressBtn()">添加地址</XtxButton>
    </div>
  </div>
   <!-- 对话框组件 - 添加和修改 -->
    <AddressEdit ref="addressEditCom" @change="getAddress"></AddressEdit>
    <!-- 切换收货地址 -->
  <XtxDialog title="切换收货地址" v-model="dialogVisible">
      <div
        class="text item"
        v-for="item in addresses"
        :key="item.id"
        :class="{ active: item.id === activeId  }"
        @click="changeAddress(item.id)"
      >
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
        </ul>
      </div>
      <template v-slot:footer>
        <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton type="primary" @click="confirmAddress">确认</XtxButton>
      </template>
  </XtxDialog>
</template>
<script>
import { computed, inject, ref, watch } from 'vue'
import AddressEdit from './address-edit.vue'
export default {
  name: 'CheckoutAddress',
  components: {
    AddressEdit
  },
  props: {
    addresses: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    const addressEditCom = ref(null)
    const data = inject('data')

    // 添加收货地址和修改收货地址
    const addAddressBtn = (address) => {
      // 把默认收货地址传递给子组件，用于展示
      addressEditCom.value.open(address)
    }

    // 新的收货地址
    const newAddress = ref(null)
    // 显示到页面中的收货地址
    const defaultAddress = computed(() => {
      // 如果没有添加或修改新的收货地址
      if (!newAddress.value) {
        return props.addresses.find(item => item.isDefault === 0) || {}
      } else {
        // 有新地址就显示新地址
        return newAddress.value
      }
    })

    // id 直接从页面显示的收货地址中获取,变化的时候也会自动监听重新获取
    watch(defaultAddress, (address) => {
      //  收集提交订单接口所需的收货地址 id
      if (address) data.addressId = address.id
    }, { immediate: true })

    // 获取用户添加或修改的地址
    const getAddress = (theval) => {
      console.log(theval)
      if (theval.receiver) {
        newAddress.value = { ...theval }
      }
    }
    // 实现默认高亮效果
    const activeId = ref(defaultAddress.value.id)
    const changeAddress = (theId) => {
      // console.log(theId)
      activeId.value = theId
    }

    // 切换收货地址
    const confirmAddress = () => {
      newAddress.value = props.addresses.find(item => item.id === activeId.value)
      dialogVisible.value = false
    }
    const dialogVisible = ref(false)
    return { addressEditCom, addAddressBtn, defaultAddress, getAddress, dialogVisible, changeAddress, activeId, confirmAddress }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
